using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace natol.GoogleReaderAPI
{
    public class GFeed
    {
        public GFeed()
        {
        }
        private string _continuation;
        
        public string Continuation
        {
            get { return _continuation; }
            set { _continuation = value; }
        }
	
        private string _id;

        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }
        private GFeedItems _items = new GFeedItems();
        
        public GFeedItems Items
        {
            get { return _items; }
            set { _items = value;}
        }
        private DateTime _lastupdated;

        public DateTime LastUpdated
        {
            get { return _lastupdated; }
            set { _lastupdated = value; }
        }

        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
	

    }
    public class GFeedItems : Collection<GFeedItem>
    {
        public delegate void ItemAddedEventHandler();
        private event ItemAddedEventHandler _ItemAdded;
        public event ItemAddedEventHandler ItemAdded
        {
            add { _ItemAdded += new ItemAddedEventHandler(value); }
            remove { _ItemAdded -= new ItemAddedEventHandler(value); }
        }
        public void OnItemAdded()
        {
            if (null != _ItemAdded)
            {
                _ItemAdded();
            }
        }
        protected override void InsertItem(int index, GFeedItem item)
        {
            this.OnItemAdded();
            base.InsertItem(index, item);
        }
    }
    public class GFeedItem
    {
    
        public GFeedItem()
        {
        }
        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _description;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        private string _id;

        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }
        private bool _read = false;

        public bool Read
        {
            get { return _read; }
            set { _read = value; }
        }
        private DateTime _published;

        public DateTime Published
        {
            get { return _published; }
            set { _published = value; }
        }
        private string _url="";

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }


        private List<string> _tags= new List<string>();

        public List<string> Tags
        {
            get { return _tags; }
            set { _tags = value; }
        }

        //reference to current reader
        //private GoogleReader _readr;

        //public GoogleReader Reader
        //{
        //    get { return _readr; }
        //    set { _readr = value; }
        //}

        [NonSerialized]
        public GFeed parentFeed;
        public GoogleReader reader;

        public void MarkAsRead(GoogleReader reader)
        {
            this.reader = reader;
            BeginToggleLastRead(true);
            
        }
        public void MarkAsUnread(GoogleReader reader)
        {
            this.reader = reader;
            BeginToggleLastRead(false);
        }
        
        void ToggleWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            bool MarkAsRead = (bool)e.Argument;
            
            bool result = reader.ToggleRead(this,MarkAsRead);
            if (result) Read = MarkAsRead;
        }
        public void BeginToggleLastRead(bool MarkAsRead)
        {
            BackgroundWorker ToggleWorker = new BackgroundWorker();
            ToggleWorker.DoWork += new DoWorkEventHandler(ToggleWorker_DoWork);
            ToggleWorker.RunWorkerAsync(MarkAsRead);
        }


    }
}
